#! python
# -*- coding: UTF-8 -*-
"""
@Project ：ai-demo 
@File    ：afr.py
@IDE     ：PyCharm 
@Author  ：zhangyiheng
@Date    ：2025/4/3 10:54 
@Describe：人脸识别
"""
from commcon.constant import MODEL_ROOT
import cv2
import numpy as np
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks

inference = pipeline(
    task=Tasks.face_recognition,  # 人脸识别任务
    model=f"{MODEL_ROOT}/bubbliiiing/cv_retinafce_recognition",
    model_revision='v1.0.3'
)
img1 = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/face_recognition_1.png'
img2 = 'https://modelscope.oss-cn-beijing.aliyuncs.com/test/images/face_recognition_2.png'
emb1 = inference(dict(user=img1))[OutputKeys.IMG_EMBEDDING]
emb2 = inference(dict(user=img2))[OutputKeys.IMG_EMBEDDING]
sim = np.dot(emb1[0], emb2[0])
print(f'Face cosine similarity={sim:.3f}, img1:{img1}  img2:{img2}')
